//取文件后缀名
function getFileExt(filepath)
{
    if (filepath != "") {
        var pos = filepath.replace(/.+\./, "");
        return pos;
    }
    return '';
}
//取文件名不带后缀
function getFileNameNoExt(filepath)
{
	var arr = filepath.split('.');
	return arr[0];
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
	  	for(var i in haystack) {
	   		if(haystack[i] == needle) {
	     		return true;
	   		}
		}
	}
	return false;
}


String.prototype.replaceAll = function(s1,s2) { 
    /*var str = this;
	while (str.indexOf(s1) != -1) {
		str = str.replace(s1, s2);
	}
	return str;*/
	
	s1 = s1.replace(/(\[)/g, "\\[");
	s1 = s1.replace(/(\])/g, "\\]");
	s1 = s1.replace(/(\()/g, "\\(");
	s1 = s1.replace(/(\))/g, "\\)");
	s1 = s1.replace(/(\*)/g, "\\*");
	
	var ret=  this.replace(new RegExp(s1, 'gm'),s2);
	return ret;
}

Array.prototype.hasVal = function(e) { 
    for(i=0; i<this.length; i++){
        if(this[i] == e){
			return true;
		}
    }
    return false;
}

String.prototype.trim = function() 
{ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.lTrim = function() 
{ 
	return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.rTrim = function() 
{ 
	return this.replace(/(\s*$)/g, ""); 
} 


$.extend($.prototype, {
	htmlCode:function(){
		var div = $('<div />');
		$(this).clone().appendTo(div);
		return div.html();
	}
}); 


function waterFall(obj, itemSelector)
{
	if(obj.hasClass('masonry')){
		obj.masonry('reload');
	}else{
		obj.masonry({
			itemSelector: itemSelector,
			singleMode: true,
			resizeable: true,
			animate: true,
			isAnimated: true,
			animationOptions: {
				duration: 390,
				easing: 'linear',
				queue: false
			},
			isFitWidth: true
		}).addClass('masonry');
	}
}



$(function(){
	$.fn.picPortionResize = function(options_events){
		var opts = $.extend({}, 
							{	
								staticSide: 'width',	//can be 'width' or 'height'
								setting: {
									staticVal: 100,   //can be a number, or 'parent'
									anotherMax: 200,  //can be a number(if 0, no limit), or 'parent', or 'sameToStaticVal'
									anotherStaticVal: '1'
								},
								keepSmaller: false
							},
							options_events);
		
		return $(this).each(function(){
			var newImg = new Image();
			newImg.src = $(this)[0].src

			var parentObj = $(this).parent();
			var paddingWidth = (parentObj.css('padding-left').replace('px', '')*1 + parentObj.css('padding-right').replace('px', '')*1);
			var paddingHeight = (parentObj.css('padding-top').replace('px', '')*1 + parentObj.css('padding-bottom').replace('px', '')*1);
			while(parentObj.css('display') != 'block'){
				parentObj = parentObj.parent();
				//parentObj.css('display', 'block');
				paddingWidth += (parentObj.css('padding-left').replace('px', '')*1 + parentObj.css('padding-right').replace('px', '')*1);
				paddingHeight += (parentObj.css('padding-top').replace('px', '')*1 + parentObj.css('padding-bottom').replace('px', '')*1);
			}
			
			if(opts.staticSide == 'width'){
				var staticVal = (opts.setting.staticVal == 'parent')? (parentObj.width() - paddingWidth) : opts.setting.staticVal;
				if(opts.setting.anotherMax == 'parent'){
					maxHeight = parentObj.innerHeight() - paddingHeight;
				}else if(opts.setting.anotherMax == 'sameToStaticVal'){
					maxHeight = staticVal;
				}else{
					maxHeight = opts.setting.anotherMax;
				}
				if(!(newImg.width < opts.setting.staticVal && opts.keepSmaller)){
					var resizeWidth = staticVal;		
					var resizeHeight = newImg.height / newImg.width * resizeWidth;
					if(resizeHeight > maxHeight && maxHeight != 0){
						resizeHeight = maxHeight;
					}
				}
				if(opts.setting.anotherStaticVal !== null && opts.setting.anotherStaticVal !== undefined){
					resizeHeight = opts.setting.anotherStaticVal;
				}
			}else{
				var staticVal = (opts.setting.staticVal == 'parent')? (parentObj.height() - paddingHeight) : opts.setting.staticVal;
				if(opts.setting.anotherMax == 'parent'){
					maxWidth = parentObj.innerWidth() - paddingWidth;
				}else if(opts.setting.anotherMax == 'sameToStaticVal'){
					maxWidth = staticVal;
				}else{
					maxWidth = opts.setting.anotherMax;
				}
				if(!(newImg.height < opts.setting.staticVal && opts.keepSmaller)){
					var resizeHeight = staticVal;
					var resizeWidth = newImg.width / newImg.height * resizeHeight;
					if(resizeWidth > maxWidth && maxWidth != 0){
						resizeWidth = maxWidth;
					}
				}
				if(opts.setting.anotherStaticVal !== null && opts.setting.anotherStaticVal !== undefined){
					resizeWidth = opts.setting.anotherStaticVal;
				}
			}
			$(this).width(resizeWidth);
			$(this).height(resizeHeight);
		});
	};
	
    $.fn.autoZindexTop = function(options_events){
		var opts = $.extend({}, 
							{	
								/*options*/
								cleanHandler: null
							},
							options_events);
				
		var objs = $(this);
		
		objs.mousedown(function(event){
			event.stopPropagation();
			
			$.each(objs, function(k, v){
				$(v).css('z-index', $(v).data('z-index'));
			});
			$(this).css('z-index', 10000);
		});
		
		opts.cleanHandler.mousedown(function(){
			$.each(objs, function(k, v){
				$(v).css('z-index', $(v).data('z-index'));
			});
		});
							
		return $(this).each(function(){
			$(this).data('z-index', $(this).css('z-index'));
		});
	};
	
	$('.auto-zindex').autoZindexTop({cleanHandler: $('body')});
});

/**
 * @author Maowl
 * 
 * 根据回传的json_endo形式的$_POST, 将值写回表单
 * 
 * usage:
 	var FormInitModel = new FormInit(<?php echo json_encode($_POST); ?>);
	FormInitModel.transverArrtoForm();
	
	如果$_POST中有数组形式的值，表单元素需要一个和name相同的class，如: <input name="group[]" class="group" />
 */
window.FormInit = function(_formData){
	this.formData = _formData;
	this.formGroup = null;
	
	this.TransverArrFunc = function (Arr, Arrvalname){
		var existCount = jQuery('.'+Arrvalname).size();
		var thisObj = this;
		
		jQuery.each(Arr, function(name, val){
			if(typeof(val) == 'object'){
				thisObj.TransverArrFunc(val, name);
			}else{
				if(Arrvalname != null){
					if(thisObj.formData['clickId'][Arrvalname] != null){
						if(name >= existCount-1){
							jQuery('#'+thisObj.formData['clickId'][Arrvalname]).click();
						}
					}
				}
				
				if(Arrvalname == null){
					if(thisObj.formGroup != null){
						name = thisObj.formGroup+'['+name+']';
					}
					jQuery('[name="'+name+'"]').val(val);
				}else{
					jQuery('.'+Arrvalname).eq(name).val(val);
				}
				
			}
		});
	};
	
	this.setFormGroup = function(_formGroup){
		this.formGroup = _formGroup;
	};
	
	this.transverArrtoForm = function(){
		this.TransverArrFunc(this.formData, null);
	}
};




